package com.hxzy.projectssmplus.security;

import com.alibaba.fastjson.annotation.JSONField;
import com.hxzy.projectssmplus.entity.SysAdminUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

/**
 * 重新实现Spring security用户登录成功的接口（用户名，密码，权限赋值）
 */
public class LoginUserDetails implements UserDetails {

    /**
     * 当前用户id
     */
    private Integer userId;

    /**
     * 当用登录的用户对象
     */
    private SysAdminUser adminUser;

    /**
     * 当前用户拥有的权限
     */
    private Set<SimpleGrantedAuthority> permissions;

    /**
     * 用户唯一标识 (uuid)
     */
    private String token;

    /**
     * 登录时间(毫秒)
     */
    private Long loginTime;

    /**
     * 过期时间(毫秒)
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 为了防止 fastjson在反序列化的时候，有些参数不能初始化的赋值的问题
     */
    public LoginUserDetails(){

    }

    /**
     * 初始化
     * @param userId  用户id
     * @param adminUser  用户对象
     * @param permissions 拥有权限
     */
    public LoginUserDetails(Integer userId, SysAdminUser adminUser, Set<SimpleGrantedAuthority> permissions)
    {
        this.userId = userId;
        this.adminUser = adminUser;
        this.permissions = permissions;
    }

    /**
     * 当前用户拥有的权限
     * Role角色权限必须以 ROLE_开头
     * menu菜单权限以  xx:xx:xx
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissions;
    }

    /**
     * 密码，一会 spring security配置密码验证策略，到时候自动调用这个方法的
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return this.adminUser.getLoginPassword();
    }

    /**
     * 当前登录用户名
     * @return
     */
    @Override
    public String getUsername() {
        return this.adminUser.getNickName();
    }

    /**
     * 账户是否未过期,过期无法验证
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     *  指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public SysAdminUser getAdminUser() {
        return adminUser;
    }

    public void setAdminUser(SysAdminUser adminUser) {
        this.adminUser = adminUser;
    }

    public Set<SimpleGrantedAuthority> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<SimpleGrantedAuthority> permissions) {
        this.permissions = permissions;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public String getIpaddr() {
        return ipaddr;
    }

    public void setIpaddr(String ipaddr) {
        this.ipaddr = ipaddr;
    }
}
